<?php
namespace Apps\Back\Controllers;
use Apps\Back\Controllers\ControllerAuth;
use Apps\Model\Admin;
use Phalcon\Filter as Filter;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class AdminController extends ControllerAuth{

    public function indexAction($currentPage=1,$name=""){
        //查询结果分页
        if($this->request->isPost()){
            $name=$this->request->getPost('name','string');
            $filter = new Filter();
            $name = $filter->sanitize($name, "trim");
            $row= Admin::find("name='$name'");
        }
        if($name){
          $row= Admin::find("name='$name'");
        }else{
          $row= Admin::find();
        } 
        $paginator   = new PaginatorModel(
          array(
              "data"  => $row,
              "limit" => 2,
              "page"  => $currentPage
          )
        );
        $page = $paginator->getPaginate();
        $this->view->setVar('page',$page);
        $this->view->setVar('name',$name);
        $this->view->setVar('currentPage',$currentPage);

    }

    public function addAction(){ 

    }

    //对添加的管理员用户过滤插入数据表
    public function addokAction(){
        $name=$this->request->getPost('name','string');
        $pwd=md5($this->request->getPost('pwd','string'));
        $acl=$this->request->getPost('acl','string');
        $state=$this->request->getPost('state','int');

        if(empty($name) || empty($acl) ||empty($state)||empty($pwd)){
              $this->jump("Admin/add","所需填写的内容不能为空");
        }

        //过滤空格
        $filter = new Filter();
        $name = $filter->sanitize($name, "trim");
        $pwd = $filter->sanitize($pwd, "trim");

        //判断管理员用户是否存在
        $admin=Admin::findFirst("name='$name'");
        if($admin){
          echo "管理员用户已经存在";
          return;
        }

      //插入数据库
        $map=new Admin();
        $map -> name="$name";
        $map -> pwd="$pwd";
        $map -> acl="$acl";
        $map -> state="$state";
        if($map->save()){
          $this->jump("admin/Admin/index","添加成功");
          return;
        }
    
    }

    //通过用户的ID把用户信息带给编辑页
    public function editAction($id,$currentPage=1,$name=""){
	      $this->view->setVar('id',$id);
	      $row=Admin::findFirst("id=$id");
	      $this->view->setVar('row',$row);
        $this->view->setVar('currentPage',$currentPage);
        $this->view->setVar('name',$name);

    }

   //对修改的编辑内容过滤，修改数据库
    public function editokAction($id,$currentPage=1,$name=""){
	       $acl=$this->request->getPost('acl','string');
	       $state=$this->request->getPost('state','int');

         if(empty($acl) ||empty($state)){
            $this->jump("admin/Admin/edit/$id/$currentPage/$name","修改内容不能为空");
         }

         //修改数据库
  	     $map=Admin::findFirst("id=$id");
	       $map->acl="$acl";
	       $map->state="$state";
	       if($map->save()){
	       	  $this->jump("admin/Admin/index/$currentPage/$name","修改成功");
	       }
	          $this->jump("admin/Admin/edit/$id/$currentPage/$name","修改不成功");

    }

   //删除管理员数据
    public function delAction($id){
         $row=Admin::findFirst("id=$id");
	       if(empty($row)){
	          $this->jump("admin/Admin/index","管理员用户名不存在");
	        }
          if($row->delete()){
        	$this->jump("admin/Admin/index","删除成功");
          }
    }


}